package com.example.dryyzx.dao;

import com.example.dryyzx.controller.OutgoingRecordController;
import com.example.dryyzx.encapsulation.ApprovalGoOutRecord;
import com.example.dryyzx.entity.OutgoingRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.ArrayList;
import java.util.Date;

public interface OutgoingRecordMapper {
    @Delete({
        "delete from outgoing_record",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into outgoing_record (id, customer_id, ",
        "companion, companion_relation, ",
        "companion_phone, reason, ",
        "out_time, return_time_forecast, ",
        "remarks, return_time_actual,approval_status,approver,approval_time)",
        "values (#{id,jdbcType=INTEGER}, #{customerId,jdbcType=INTEGER}, ",
        "#{companion,jdbcType=VARCHAR}, #{companionRelation,jdbcType=VARCHAR}, ",
        "#{companionPhone,jdbcType=VARCHAR}, #{reason,jdbcType=VARCHAR}, ",
        "#{outTime,jdbcType=TIMESTAMP}, #{returnTimeForecast,jdbcType=TIMESTAMP}, ",
        "#{remarks,jdbcType=VARCHAR}, #{returnTimeActual,jdbcType=TIMESTAMP},",
        "#{approvalStatus,jdbcType=CHAR},#{approver,jdbcType=VARCHAR},#{approvalTime,jdbcType=TIMESTAMP})"
    })
    int insert(OutgoingRecord record);

    @InsertProvider(type=OutgoingRecordSqlProvider.class, method="insertSelective")
    int insertSelective(OutgoingRecord record);

    @Select({
        "select",
        "id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
        "return_time_forecast, remarks, return_time_actual, approval_status, approver, approval_time",
        "from outgoing_record",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR), 
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)

    })
    OutgoingRecord selectByPrimaryKey(@Param("id") Integer id);

    @Select({
            "select",
            "id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
            "return_time_forecast, remarks, return_time_actual, approval_status, approver, approval_time",
            "from outgoing_record",
            "where approval_status = #{status,jdbcType=CHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR), 
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)

    })
    ArrayList<OutgoingRecord> selectRecordByApprovalStatus(@Param("status") String status);

    @Select({
            "select",
            "outgoing_record.id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
            "return_time_forecast, outgoing_record.remarks, return_time_actual, approval_status, approver, approval_time",
            "from outgoing_record , `customer`",
            "where approval_status = #{status,jdbcType=CHAR} and customer.name like concat('%',#{uname,jdbcType=VARCHAR},'%')",
            "and outgoing_record.customer_id = customer.id"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
            @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
            @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
            @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
            @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
            @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
            @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
            @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR),
            @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)

    })
    ArrayList<OutgoingRecord> selectRecordByApprovalStatusAndName(@Param("uname") String uname,@Param("status") String status);



    @Select({
            "select",
            "id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
            "return_time_forecast, remarks, return_time_actual, approval_status ,approver, approval_time",
            "from outgoing_record",
            "where customer_id = #{customerId,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR), 
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)
    })
    OutgoingRecord selectByCustomerId(@Param("customerId") Integer customerId);

    @Select({
            "select",
            "id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
            "return_time_forecast, remarks, return_time_actual, approval_status, approver, approval_time",
            "from outgoing_record",
            "where out_time like concat(#{outTime,jdbcType=TIME},'%')"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR), 
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)
    })
    ArrayList<OutgoingRecord> selectByOutTimeDate(@Param("outTime") String outTime);//只根据年月日查找记录

    @Select({
            "select",
            "id, customer_id, companion, companion_relation, companion_phone, reason, out_time,",
            "return_time_forecast, remarks, return_time_actual, approval_status, approver, approval_time",
            "from outgoing_record"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="customer_id", property="customerId", jdbcType=JdbcType.INTEGER),
        @Result(column="companion", property="companion", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_relation", property="companionRelation", jdbcType=JdbcType.VARCHAR),
        @Result(column="companion_phone", property="companionPhone", jdbcType=JdbcType.VARCHAR),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="out_time", property="outTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="return_time_forecast", property="returnTimeForecast", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="remarks", property="remarks", jdbcType=JdbcType.VARCHAR),
        @Result(column="return_time_actual", property="returnTimeActual", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="approval_status", property="approvalStatus", jdbcType=JdbcType.CHAR),
        @Result(column="approver", property="approver", jdbcType=JdbcType.VARCHAR), 
        @Result(column="approval_time", property="approvalTime", jdbcType=JdbcType.TIMESTAMP)
    })
    ArrayList<OutgoingRecord> selectAll();//查找所有

    @UpdateProvider(type=OutgoingRecordSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(OutgoingRecord record);

    @Update({
        "update outgoing_record",
        "set customer_id = #{customerId,jdbcType=INTEGER},",
          "companion = #{companion,jdbcType=VARCHAR},",
          "companion_relation = #{companionRelation,jdbcType=VARCHAR},",
          "companion_phone = #{companionPhone,jdbcType=VARCHAR},",
          "reason = #{reason,jdbcType=VARCHAR},",
          "out_time = #{outTime,jdbcType=TIMESTAMP},",
          "return_time_forecast = #{returnTimeForecast,jdbcType=TIMESTAMP},",
          "remarks = #{remarks,jdbcType=VARCHAR},",
          "return_time_actual = #{returnTimeActual,jdbcType=TIMESTAMP}",
          "approval_status = #{approvalStatus,jdbcType=CHAR}",
          "approver = #{approver,jdbcType=VARCHAR}",
          "approval_time = #{approvalTime,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(OutgoingRecord record);
}